package cn.csc.module.shoppingCard.service.points;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.shoppingCard.controller.admin.points.vo.CardPointsCreateReqVO;
import cn.csc.module.shoppingCard.controller.admin.points.vo.CardPointsExportReqVO;
import cn.csc.module.shoppingCard.controller.admin.points.vo.CardPointsPageReqVO;
import cn.csc.module.shoppingCard.controller.admin.points.vo.CardPointsUpdateReqVO;
import cn.csc.module.shoppingCard.convert.points.CardPointsConvert;
import cn.csc.module.shoppingCard.dal.dataobject.points.CardPointsDO;
import cn.csc.module.shoppingCard.dal.mysql.points.CardPointsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.shoppingCard.enums.ErrorCodeConstants.CARD_POINTS_NOT_EXISTS;

/**
 * 购物卡积分 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class CardPointsServiceImpl extends ServiceImpl<CardPointsMapper, CardPointsDO> implements CardPointsService {

    @Override
    public BigInteger createCardPoints(CardPointsCreateReqVO createReqVO) {
        // 插入
        CardPointsDO cardPoints = CardPointsConvert.INSTANCE.convert(createReqVO);
        baseMapper.insert(cardPoints);
        // 返回
        return cardPoints.getId();
    }

    @Override
    public void updateCardPoints(CardPointsUpdateReqVO updateReqVO) {
        // 校验存在
        validateCardPointsExists(updateReqVO.getId());
        // 更新
        CardPointsDO updateObj = CardPointsConvert.INSTANCE.convert(updateReqVO);
        baseMapper.updateById(updateObj);
    }

    @Override
    public void deleteCardPoints(BigInteger id) {
        // 校验存在
        validateCardPointsExists(id);
        // 删除
        baseMapper.deleteById(id);
    }

    private void validateCardPointsExists(BigInteger id) {
        if (baseMapper.selectById(id) == null) {
            throw exception(CARD_POINTS_NOT_EXISTS);
        }
    }

    @Override
    public CardPointsDO getCardPoints(BigInteger id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<CardPointsDO> getCardPointsList(Collection<BigInteger> ids) {
        return baseMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CardPointsDO> getCardPointsPage(CardPointsPageReqVO pageReqVO) {
        return baseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CardPointsDO> getCardPointsList(CardPointsExportReqVO exportReqVO) {
        return baseMapper.selectList(exportReqVO);
    }

}
